import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core';
import { NbThemeService } from '@nebular/theme';
import { BasicService } from '../../../@core/data/basic.service';

@Component({
  selector: 'ngx-myscore-bar',
  template: `
    <div echarts [options]="options" [loading]="!isLoaded" class="echart" ></div>
  `,
})
export class MyScoreBarComponent implements AfterViewInit, OnDestroy {
  options: any = {};
  themeSubscription: any;
  isLoaded: boolean;

  constructor(private service: BasicService, private theme: NbThemeService) {

  }
  ngAfterViewInit() {
    this.themeSubscription = this.theme.getJsTheme().subscribe(config => {
      const colors: any = config.variables;
      const echarts: any = config.variables.echarts;
      const _self = this;
      const getAchievements = function () {
        return new Promise(function (resolve, reject) {
          _self.service.getOne('achievements').subscribe((data: any) => {
            resolve(data.rows);
          });
        });
      };
      (async () => {
        const rows = await getAchievements() as Array<any>;
        const categoryValList = [];
        const dataValList = [];
        if (rows && rows.length > 0) {
          for (const a of rows) {
            categoryValList.push(a.paperName);
            dataValList.push(a.score);
          }
        }
        // hide loading:
        this.isLoaded = true;
        this.options = {
          backgroundColor: echarts.bg,
          color: [colors.primaryLight],
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
            },
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: [
            {
              type: 'category',
              data: categoryValList,
              axisTick: {
                alignWithLabel: true,
              },
              axisLine: {
                lineStyle: {
                  color: echarts.axisLineColor,
                },
              },
              axisLabel: {
                textStyle: {
                  color: echarts.textColor,
                },
              },
            },
          ],
          yAxis: [
            {
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: echarts.axisLineColor,
                },
              },
              splitLine: {
                lineStyle: {
                  color: echarts.splitLineColor,
                },
              },
              axisLabel: {
                textStyle: {
                  color: echarts.textColor,
                },
              },
            },
          ],
          series: [
            {
              name: '成绩',
              type: 'bar',
              barWidth: '60%',
              data: dataValList,
            },
          ],
        };
      })();
    });
  }

  ngOnDestroy(): void {
    this.themeSubscription.unsubscribe();
  }
}
